<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
   function red() {
    console.log('红灯亮了');
}
function green() {
    console.log('绿灯亮了');
}
function yellow() {
    console.log('黄灯亮了');
}
//设置红绿灯亮点亮的方法和时间
function changelight(light, time) {
    return new Promise((resolve) => {
        setTimeout(() => {
            light();
            resolve();
        }, time);
    });
}
function loop() {
    return changelight(red, 3000)//红灯亮 3 秒
       .then(() => changelight(green, 2000))// 绿灯亮 2 秒
       .then(() => changelight(yellow, 1000))//黄灯亮 1 秒
       .then(() => loop());
}

//开始循环亮灯
loop();
</script>

</html>